using System;
using System.Collections.Generic;
using System.Threading;
using Catastrophe.Common.ObjM;

namespace LibUtil.ObjM
{
    public class VarMonitor : IDisposable
    {
        private unsafe bool*[] _buf;

        private readonly ControlLogic<bool> _isAndOprChanged;

        private readonly ControlLogic<bool> _isOrOprChanged;

        private Thread _tdMonitor;

        private List<dynamic> _list = new List<object>();

        public Action AndTrueHandlerOnce { get; set; }

        public Action OrTrueHandlerOnce { get; set; }

        public Action<bool> AndTrueHandler { get; set; }

        public Action<bool> OrTrueHandler { get; set; }

        public bool Result { get; set; }

        public unsafe VarMonitor(int maxCount = 1024)
        {
            this._buf = new bool*[maxCount];
            this._isAndOprChanged = new ControlLogic<bool>();
            this._isOrOprChanged = new ControlLogic<bool>();
        }

        public void TdStart()
        {
            if (this._tdMonitor == null)
            {
                this._tdMonitor = new Thread(new ThreadStart(CheckResult))
                {
                    IsBackground = true
                };
                this._tdMonitor.Start();
            }
        }

        unsafe ~VarMonitor()
        {
            this._buf = null;

        }

        public unsafe void Dispose()
        {
            this._tdMonitor.Abort();
            this._buf = null;
            GC.Collect();
        }

        private void CheckResult()
        {
            while (true)
            {
                bool flag = this.VarAndMonitor();
                this.AndTrueHandler?.Invoke(flag);
                if (this._isAndOprChanged.IsDataChange(flag) && flag)
                {
                    this.AndTrueHandlerOnce?.Invoke();
                }
                bool flag2 = this.VarOrMonitor();
                this.OrTrueHandler?.Invoke(flag2);
                if (this._isOrOprChanged.IsDataChange(flag2) && flag2)
                {
                    this.OrTrueHandlerOnce?.Invoke();
                }
                Thread.Sleep(50);
            }
        }

        public unsafe void VarLifeAdd(ref bool value)
        {
            fixed (bool* ptr = &value)
            {
                for (int i = 0; i < this._buf.Length; i++)
                {
                    if (this._buf[i] == null)
                    {
                        this._buf[i] = ptr;
                        return;
                    }
                    if (this._buf[i] == ptr)
                    {
                        return;
                    }
                }
            }
        }

        public unsafe bool VarAndMonitor()
        {
            bool flag = true;
            bool flag2 = true;
            bool*[] buf = this._buf;
            foreach (bool* ptr in buf)
            {
                if (ptr != null)
                {
                    flag2 = false;
                    bool flag3 = *ptr;
                    flag = flag && *ptr;
                }
            }
            return !flag2 && flag;
        }

        public unsafe bool VarOrMonitor()
        {
            bool flag = false;
            bool*[] buf = this._buf;
            foreach (bool* ptr in buf)
            {
                if (ptr != null)
                {
                    bool flag2 = *ptr;
                    flag = flag || *ptr;
                }
            }
            return flag;
        }
    }
}
